<template>
  <el-dialog title="添加"
             :close-on-click-modal="false"
             :visible.sync="dialogShow"
             width="540px"
             :before-close="handleClose">
    <el-form :model="dialogForm"
             ref="dialogForm"
             :rules="rules"
             label-width="100px">
      <el-form-item label="检查内容："
                    prop="content">
        <el-input v-model.trim="dialogForm.content"
                  size="small"
                  style="width: 215px;"
                  placeholder="请输入检查内容"></el-input>
      </el-form-item>
      <el-form-item label="评分："
                    prop="score">
        <el-input-number v-model="dialogForm.score"
                         controls-position="right"
                         :min="0"
                         :max="100"
                         :disabled="dialogForm.checkType === 3"
                         size="small"
                         type="number"
                         style="width: 215px;"
                         placeholder="请输入评分"></el-input-number>
      </el-form-item>
      <el-form-item label="检查类型："
                    prop="checkType">
        <el-select v-model="dialogForm.checkType"
                   @change="typeChange"
                   size="small"
                   placeholder="请选择检查类型">
          <el-option v-for="(item, index) in typeList"
                     :key="index"
                     :label="item"
                     :value="index + 1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="handleClose"
                 size="small">取 消</el-button>
      <el-button type="primary"
                 @click="sure"
                 size="small">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { _addCheckProjectItem } from '@api/systemManage'
export default {
  props: {
    dialogShow: Boolean,
    id: Number
  },
  data () {
    return {
      dialogForm: {
        content: '',
        score: null,
        checkType: null
      },
      rules: {
        content: [
          { required: true, message: '请输入检查内容', trigger: 'blur' },
          { pattern: /^.{1,50}$/, message: '最多输入50个字符' }
        ],
        score: [
          { required: true, message: '请输入评分', trigger: 'blur' }
        ],
        checkType: [
          { required: true, type: 'number', message: '请选择检查类型' }
        ]
      },
      typeList: ['卫生检查', '违章检查', '禁止项目']
    }
  },
  methods: {
    handleClose () {
      this.$refs.dialogForm.resetFields()
      this.$emit('handleClose')
    },
    sure () {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          _addCheckProjectItem({
            checkType: this.dialogForm.checkType,
            score: this.dialogForm.score,
            content: this.dialogForm.content,
            projectId: this.id
          }).then(res => {
            this.$emit('sure')
            this.handleClose()
          })
        }
      })
    },
    typeChange () {
      if (this.dialogForm.checkType === 3) {
        this.dialogForm.score = 0
      }
    },
    placeChange (val) {
      console.log(val, 'checkType')
    }
  }
}
</script>
